﻿using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using SLS.ExClassLib.Database;
namespace SLS.Base
{
    public partial class ExDATA
    { 
        public CollectionViewSource vwGoodsMasterKeyRelation
        {
            get
            { 
                return (CollectionViewSource)this.TryFindResource("vwGoodsMasterKeyRelation");
            }
        }
        public TextBlock KSM
        {
            get
            {
                return (TextBlock)FindName("KSM");
            }
        }
        public TextBlock KKEYDEFTYPE
        {
            get
            {
                return (TextBlock)FindName("KKEYDEFTYPE");
            }
        }
        public TextBlock KGTPRE
        {
            get
            {
                return (TextBlock)FindName("KGTPRE");
            }
        }
        public TextBlock KGTKEY
        {
            get
            {
                return (TextBlock)FindName("KGTKEY");
            }
        }
        public TextBlock KKEYINSTANCETABLE
        {
            get
            {
                return (TextBlock)FindName("KKEYINSTANCETABLE");
            }
        }
        public ComboBox KPOSITIONID
        {
            get
            {
                return (ComboBox)FindName("KPOSITIONID");
            }
        }
        public ComboBox mGDKEY
        {
            get
            {
                return (ComboBox)FindName("mGDKEY");
            }
        }
        public ComboBox sGDKEY
        {
            get
            {
                return (ComboBox)FindName("sGDKEY");
            }
        }
        public DataRow dwGDKEY
        {
            get
            {
                return ActiveDataRow.Item;
            }
        }
        public DataRow dwGoodsMasterKeyRelation
        {
            get { return ActiveDataRow.dwGoodsMasterKeyRelation; }
        }
        string MGDKEY 
        {
              get
            {
                return mGDKEY.Text;
            }
            set
            {
                mGDKEY.Text = value;
            }
        }
        string SGDKEY 
        {
              get
            {
                return sGDKEY.Text;
            }
            set
            {
                sGDKEY.Text = value;
            }
        }
 
        string SM
        {
            get
            {
                return dwGoodsMasterKeyRelation["SM"].ToString();
            }
        }
        string MKEYINSTANCETABLE
        {
            get
            {
                return dwGoodsMasterKeyRelation["MKEYINSTANCETABLE"].ToString();
            }
        }
        string MKEYDEFTYPE
        {
            get
            {
                return dwGoodsMasterKeyRelation["MKEYDEFTYPE"].ToString();
            }
        }
        string MGTPRE
        {
            get
            {
                return dwGoodsMasterKeyRelation["MGTPRE"].ToString();
            }
        }
        string MGTKEY
        {
            get
            {
                return dwGoodsMasterKeyRelation["MGTKEY"].ToString();
            }
        }
        string SKEYINSTANCETABLE
        {
            get
            {
                return dwGoodsMasterKeyRelation["SKEYINSTANCETABLE"].ToString();
            }
        }
        public virtual void mGDKEY_LostFocus(object sender, RoutedEventArgs e)
        { 
            try
            {
             
                if (MGDKEY == "")
                    return;
                if (MGDKEY == "1@2=1")
                    return;
               string KeyCondition = MKEYINSTANCETABLE + "." + SM + "." + MKEYDEFTYPE + "." + MGTPRE + "." + MGTKEY + "." + MGDKEY;
            
               if (SKEYINSTANCETABLE == "SysKeyData")
                {
                    if (SGDKEY != "")
                        return;
                    
                    int newValue = ExRowBase.GetMaxIDSysKeyData(MKEYINSTANCETABLE, KeyCondition);
                    SGDKEY = newValue.ToString();
                } 
               UpdateCombinationKey();
        
            }
            catch (Exception ex)
            {
                throw;
            }     
        }
        private void UpdateCombinationKey()
        {
            try
            {
                string GDKEY = "";
                if (MKEYINSTANCETABLE == "KeyBoardInput"
                    && MGDKEY.Trim() == "")
                    return;

                if (MKEYINSTANCETABLE != "KeyBoardInput"
                    && MGDKEY.Trim() != ""
                    && SGDKEY.Trim() == ""
                    )
                    return;
                if (SGDKEY.Trim() == "")
                {
                    GDKEY = MGDKEY;
                }
                else
                {
                    string Line0 = dwGoodsMasterKeyRelation["Line"].ToString().Trim();
                    if (Line0 == "0")
                    {
                        GDKEY = MGDKEY
                            + "." + SGDKEY
                            ;
                    }
                    else
                    {
                        GDKEY = MGDKEY
                            + "." + SGDKEY
                            + "." + Line0;
                    }
                }
                string GDKEYID=dwGoodsMasterKeyRelation["GDKEYID"].ToString();
                dwGDKEY["GDKEYID"] = GDKEYID;
                string TBName=dwGDKEY.Table.TableName;
                if (dwGDKEY.Table.Columns.Contains(TBName.ToUpper() + "ID"))
                {
                    dwGDKEY[TBName.ToUpper() + "ID"] = GDKEYID;
                }
                if (dwGDKEY.Table.Columns.Contains(TBName.ToUpper() + "KEY"))
                {
                    dwGDKEY[TBName.ToUpper() + "KEY"] = GDKEY;
                }
                dwGoodsMasterKeyRelation["MGDKEY"] = MGDKEY;
                dwGoodsMasterKeyRelation["SGDKEY"] = SGDKEY;
                dwGoodsMasterKeyRelation["GDKEY"] = GDKEY;
                ActiveDataRow.GDKEY = GDKEY;                
                ActiveDataRow.OnSaveRecord(2); 
                DataSet setEvent = new DataSet();
                setEvent = (DataSet)DataAccessService.Instance.ImportDataTable(setEvent, ActiveDataRow.Item);
                SetUpTableBase(setEvent, true);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public virtual void sGDKEY_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            { 
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public virtual void cmbKeyRelation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
        }  
    }
}
